package com.example.lance.textapp;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.view.KeyEvent;

import java.util.Vector;

/**
 * Created by lance on 15/5/8.
 */
public class TextInRectUtil {

    private Rect mRect = null;
    private int mRectw = 0;//Rect长度
    private int mRecth = 0;//Rect高度

    private String mStrText = "";
    private Vector mString = null;
    private Paint mPaint = null;

    private int mFontHeight = 0;//字体高度
    private int mRealLine = 0;//排版行数
    private int mTextw = 0;//文字框的长度
    private int mTexth = 0;//文字框的高度
    private int x = 0;//文字框的x坐标
    private int y = 0;//文字框的y坐标

    public TextInRectUtil(String StrText, Rect rect, Paint paint) {
        mStrText = StrText;
        mPaint = paint;
        mString = new Vector();

        mRect = rect;
        mRectw = (int) Math.abs(mRect.left - mRect.right);//矩形长度
        mRecth = (int) Math.abs(mRect.top - mRect.bottom);//矩形高度

        measure();
        layout();
    }

    /**
     * 测量
     */
    private void measure() {
        char ch;
        int w = 0;//文字图像的长度
        int istart = 0;
        FontMetrics fm = mPaint.getFontMetrics();// 得到系统默认字体属性
        mFontHeight = (int) (Math.ceil(fm.descent - fm.top) + 2);// 获得字体高度
        int count = this.mStrText.length();
        for (int i = 0; i < count; i++) {
            ch = this.mStrText.charAt(i);
            float[] widths = new float[1];
            String str = String.valueOf(ch);
            mPaint.getTextWidths(str, widths);
            if (ch == '\n') {
                //判断高度有木有越界
                if (mRecth < mFontHeight * (mRealLine + 1))
                    break;
                mRealLine++;// 真实的行数加一

                mString.addElement(this.mStrText.substring(istart, i));
                istart = i + 1;
                w = 0;
            } else {
                w += (int) Math.ceil(widths[0]);
                //判断长度有木有越界，越界转下一行
                if (w > mRectw) {
                    //判断高度有木有越界
                    if (mRecth < mFontHeight * (mRealLine + 1))
                        break;
                    mRealLine++;// 真实的行数加一

                    mString.addElement(this.mStrText.substring(istart, i));
                    istart = i;
                    i--;
                    w = 0;
                } else {
                    if (i == count - 1) {
                        //计算mTextw
                        if (w > mTextw)
                            mTextw = w;

                        //判断高度有木有越界
                        if (mRecth < mFontHeight * (mRealLine + 1))
                            break;
                        mRealLine++;// 真实的行数加一

                        mString.addElement(this.mStrText.substring(istart, count));
                    }
                }
            }
        }

        //计算mTexth
        mTexth = mFontHeight * mRealLine;
    }


    /**
     * 布局
     */
    private void layout() {

        FontMetrics fm = mPaint.getFontMetrics();// 得到系统默认字体属性
        int fontHeight = (int) Math.ceil(fm.descent - fm.ascent);
        if (mTexth < mRecth) {
            y = mRect.top + (mRecth - mTexth) / 2 + fontHeight;
        } else {
            y = mRect.top + fontHeight;
        }

        Paint.Align align = mPaint.getTextAlign();
        if (align == Paint.Align.LEFT) {
            x = mRect.left;
        } else if (align == Paint.Align.CENTER) {
            x = mRect.left + (mRect.right - mRect.left) / 2;
        } else if (align == Paint.Align.RIGHT) {
            x = mRect.right;
        }
    }

    /**
     * 绘制字符串
     *
     * @param canvas
     */
    public void drawText(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        canvas.drawCircle(x, y, 5, paint);

        Paint paintRect = new Paint();
        paintRect.setColor(Color.BLUE);
        paintRect.setStyle(Paint.Style.STROKE);
        canvas.drawRect(mRect, paintRect);

        Paint paintLine = new Paint();
        paintLine.setColor(Color.BLUE);
        canvas.drawLine(mRect.left, mRect.top + mRecth / 2, mRect.right, mRect.top + mRecth / 2, paintLine);

        for (int i = 0, j = 0; i < mRealLine; i++, j++) {
            canvas.drawText((String) (mString.elementAt(i)), x, y + mFontHeight * j, mPaint);
        }
    }

}

